package com.report.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.report.dto.request.CensusReq;
import com.report.dto.request.FileReq;
import com.report.dto.response.CensusRes;
import com.report.dto.response.FileRes;
import com.report.file.PdfFile;
import com.report.service.IFileService;
import com.report.service.UploadFileService;
import com.zmh.framework.config.BaseCommonInterface;
import com.zmh.framework.exception.ResultException;
import com.zmh.framework.util.Result;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author summer
 * @since 2024/06/25 14:54:47
 * 文件
 */
@RestController
@RequestMapping("/file")
@Slf4j
@AllArgsConstructor
public class FileController implements BaseCommonInterface<FileReq, FileRes> {

    private final UploadFileService uploadFileService;

    private final IFileService fileService;

    @PostMapping(value = "/uploadFile")
    public Result<String> uploadFile(@RequestParam("file") MultipartFile[] file) {
        log.info("file = {}", file.length);
        if (file == null || file.length == 0) {
            throw new ResultException("请选择上传文件") ;
        }
        List<PdfFile> pdfFileList = uploadFileService.uploadPDFList(file);
        fileService.saveFile(pdfFileList);
        return Result.successResult();
    }

    //TODO 上传zip
    @PostMapping(value = "/uploadZip")
    public Result<String> uploadZip(@RequestParam("file") MultipartFile file) {

        return Result.successResult();
    }


    @Override
    public Result<String> save(@Valid FileReq fileReq) {
        return null;
    }

    @Override
    public Result<String> del(Integer id) {
        fileService.removeById(id);
        return Result.successResult();
    }

    @Override
    public Result<Page<FileRes>> lists(FileReq fileReq) {
        return Result.successResult(fileService.lists(fileReq));
    }

    @Override
    public Result<FileRes> findById(Integer id) {
        return null;
    }

    @Override
    public Result<String> batchDel(List<FileRes> list) {
        return null;
    }

    @GetMapping(value = "/findLists")
    public Result<List<FileRes>> findLists(){

        return Result.successResult(fileService.findLists());
    }

    @PostMapping(value = "/census")
    public Result<Page<CensusRes>> censusLists(@RequestBody CensusReq censusReq){
        return Result.successResult(fileService.censusRes(censusReq));
    }

    @PostMapping(value = "/item")
    public Result<Page<FileRes>>  itemLists(@RequestBody CensusReq censusReq){
        log.info("censusReq = {}", censusReq);
        Page<FileRes> fileResPage= fileService.itemLists(censusReq);
        return Result.successResult(fileResPage);
    }

    @PostMapping(value = "/deleteItem")
    public Result<String> deleteItem(@RequestBody CensusReq censusReq){
        fileService.deleteItem(censusReq);
        return Result.successResult();
    }

}
